package shuishijie.suishijie.model.web;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;



import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.logging.HttpLoggingInterceptor;
import shuishijie.suishijie.BaseActivity;
import shuishijie.suishijie.WebViewActivity;
import shuishijie.suishijie.fragment.BaseFragment;
import shuishijie.suishijie.fragment.OrderPayFragment;
import shuishijie.suishijie.model.exception.ApiException;
import shuishijie.suishijie.view.WebBrowseActivity;

/**
 * Created by mindx on 2017/8/28.
 */

public class RxSubscriber<T> implements Observer<T> {

    private final OnNextListener<T> listener;
    Context context;

    public RxSubscriber(OnNextListener<T> listener) {
        this.listener = listener;
    }
   BaseFragment fragment;
    BaseActivity activity;
//
    public RxSubscriber(Object context, OnNextListener<T> listener) {
        if (context instanceof BaseFragment) {
            this.fragment = (BaseFragment) context;
        } else if (context instanceof BaseActivity) {
            this.activity = (BaseActivity) context;
        } else if (context instanceof Context) {
            this.context = (Context) context;
        } else if (context instanceof View) {
            this.context = ((View) context).getContext();
        }
        this.listener = listener;
    }

    public RxSubscriber(BaseActivity activity, OnNextListener<T> listener) {
        this.activity = activity;
        this.context = activity;
        this.listener = listener;
    }

    public RxSubscriber(BaseFragment fragment, OnNextListener<T> listener) {
        this.fragment = fragment;
        this.context = fragment.getActivity();
        this.listener = listener;
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof ApiException) {
            onError((ApiException) e);
        } else {
            ApiException ex = new ApiException(e, 10099);
            ex.setDisplayMessage("未知错误");
            onError(ex);
        }
    }

    protected void onError(ApiException ex) {
        Context context = getContext();
        if (context != null) {
         //   TCAgent.onError(context, new Exception(ex.getDisplayMessage(), ex.getCause()));
        }
//        Logger.e("onError " + ex.getLocalizedMessage() + "  " + ex.getDisplayMessage());
        if (ex.getCode() == 12 || ex.getCode() == 11) {
           // AppDBHelper.getInstance().setConfigValue("USER_INFO", "");
           // Logger.w("The application is killed. So restart from guide page");
            if (getContext() != null) {
                Intent i = new Intent(getContext(), WebBrowseActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(i);
                if (getContext() instanceof Activity) {
                    ((Activity) getContext()).finish();
                }
            }
        }
//        ex.printStackTrace();
        showMsg(ex.getDisplayMessage());
        onComplete();
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (getContext() != null) {
           // LoadingDialog.show(getContext(), "正在加载数据");
        }
    }

    private Context getContext() {
        if (activity != null) {
            return activity;
        } else if (context != null) {
            return context;
        }  else {
            return null;
      }
    }

    @Override
    public void onNext(T t) {
        if (listener != null) {
            try {
                listener.onNext(t);
            } catch (Exception e) {
                e.printStackTrace();
                Context context = getContext();
                if (context != null) {
                    //TCAgent.onError(context, e);
                }
            }
        }
    }

    @Override
    public void onComplete() {
       // LoadingDialog.dismiss();
    }

    private void showMsg(String msg) {
       // HttpLoggingInterceptor.Logger.e(msg);
        if (fragment != null) {
            fragment.showMessage(msg);
        } else if (activity != null) {
            activity.showMessage(msg);
        } else if (getContext() != null) {
            Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
        } else {

        }
    }

    public interface OnNextListener<T> {
        public void onNext(T t);
    }

}